package com.sduept.nwld.dataserver.rest;

import com.sduept.bigdata.fault.common.ParamDesc;
import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.core.ParamManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.bigdata.weather.map.MapDataCache;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 图形REST服务
 */
@RestController
@RequestMapping("/")
public class MapServices {
	
	@Autowired
	private SuperDAO dao;
	@Autowired
	private MapDataCache cache;
	@Autowired
	private ParamManager pc;
	
	/**
	 * 获取地图服务的链接
	 * @return
	 */

    @GetMapping("/getMapUrl")
	public String getMapUrl(){
		String s =  pc.get(ParamDesc.MAP_SERVICE_ADDRESS).getValue();
		s="{'mapURL':'"+s+"'}";
		return JSONObject.fromObject(s).toString();
	}
	/**
	 * 获取故障滚动条的刷新时长
	 * @return
	 */
	
    @GetMapping("/getRefreshTime")
	public String getRefreshTime(){
		String s =  pc.get(ParamDesc.FAULT_REFRESH_TIME).getValue();
		s="{'refreshTime':'"+s+"'}";
		return JSONObject.fromObject(s).toString();
	}

	/**
	 * 获取地图变电站数据
	 * @param volatge
	 * @return
	 */
	
	@GetMapping("/getStations/{volatge}")
	public String getStations(@PathVariable("volatge") int volatge) {
		JSONArray rtn = new JSONArray();
		if(volatge == 220){
			rtn.addAll(cache.getS220List());
		} else if(volatge == 500){
			rtn.addAll(cache.getS500List());
			//TODO:为了演示台风，暂时增加的
			//rtn.addAll(cache.getS220List());
		} else if(volatge == 800){
			rtn.addAll(cache.getS800List());
		}
		return rtn.toString();
	}
	
	/**
	 * 获取线路杆塔数据
	 * @param voltage
	 * @return
	 */
	
	@GetMapping("/getLineByVoltage/{voltage}")
	public String getLineTowerByVoltage(@PathVariable("voltage") int voltage) {
		if(voltage == 500){
			return cache.getMapdata500Json();
		}
        return "[]";
	}
	
	
	/**
	 * 根据名称获取线路故障信息
	 * map.js调用，已废弃
	 * @param name
	 * @return
	 */
	
	@GetMapping("/getLineFaultCountByName/{name}")
	public String getLineFaultCountByName(@PathVariable("name") String name) {
		List<FaultRecord> fs  = new ArrayList<>();
		if(!StringUtils.isEmpty(name)){
			fs = dao.findByCondition(FaultRecord.class,
					" equipment = ? and status = 0 ", name);
		}
		return com.alibaba.fastjson.JSONArray.toJSONString(fs);
	}
	/**
	 * 获取城市名称时使用
	 * @return
	 */
	
	@GetMapping("/getCountyNames")
	public String getCountyNames() {
		String sql = "SELECT `city` FROM `nwld_data_configure_db`.`t_city_wf_location_info` ORDER BY `province`,`city`";
//		String sql = "select t2.city from t_city_wf_location_info t1  left join t_city_boundary t2 on t1.county=t2.county where t2.county_boundary is null;";
		JSONArray rtn = new JSONArray();
		List<?> objs = dao.nativeQuery(sql);
		rtn.addAll(objs);
		return rtn.toString();
	}
	/**
	 * 获取城市边界信息
	 * @param voltage 500kV
	 * @return
	 */

	@PostMapping("/saveCountyBoundarys")
	public String saveCountyBoundarys(@RequestBody String str) {
		JSONObject obj = JSONObject.fromObject(str);
		String name = obj.getString("name");
		String pstr = obj.getJSONArray("points").toString();
		//String sql = "INSERT INTO `nwld_data_configure_db`.`t_city_boundary` (`county`, `county_boundary`) VALUES (?, ?)";
	//	String sql = "update `nwld_data_configure_db`.`t_city_boundary` "
	//			+ "set `county_boundary` = ? where `county_boundary` is null and `county` = ? ";
		List<Object[]> datas  = new ArrayList<>();
		datas.add(new Object[]{pstr,name});
		//log.info(pstr);
	//	try {
	//		dao.persistentBatch(sql,datas);
	//	} catch (SQLException e) {
	//		e.printStackTrace();
	//	}
		return "ok";
	}
	/**
	 * 保护日报查询的最大时间范围
	 * @return
	 */
	
	@GetMapping("/getDailyReportQueryMaxDay")
	public String getDailyReportQueryMaxDay(){
		String s =  pc.get(ParamDesc.DAILY_REPORT_QUERY_MAX_DAY).getValue();
		s="{'day':'"+s+"'}";
		return JSONObject.fromObject(s).toString();
	}
	
	@GetMapping("/reloadCimCache")
	public String reloadCimCache(){
		cache.reload();
		return "ok";
	}
}
